/*
 * (C) Copyright 2014-2018, by Dimitrios Michail
 *
 * JHeaps Library
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jheaps.monotone

import org.jheaps.Heap
import org.junit.Assert
import org.junit.Test
import java.io.*
import java.util.*

class IntegerRadixHeapTest {
    @Test
    fun testBug2() {
        val h: Heap<Int> = IntegerRadixHeap(0, 100)
        h.insert(0)
        Assert.assertEquals(0, h.findMin().toLong())
        Assert.assertEquals(0, h.deleteMin().toLong())
        h.insert(15)
        Assert.assertEquals(15, h.findMin().toLong())
    }

    @Test
    fun test() {
        val h: Heap<Int> = IntegerRadixHeap(0, SIZE)

        for (i in 0 until SIZE) {
            h.insert(i)
            Assert.assertEquals(0, h.findMin())
            Assert.assertFalse(h.isEmpty)
            Assert.assertEquals(h.size(), (i + 1).toLong())
        }

        for (i in SIZE - 1 downTo 0) {
            Assert.assertEquals(h.findMin().toLong(), (SIZE - i - 1).toLong())
            h.deleteMin()
        }
    }

    @Test
    fun testVerySmall() {
        val h: Heap<Int> = IntegerRadixHeap(29, 36)

        h.insert(29)
        h.insert(30)
        h.insert(31)
        h.insert(30)
        h.insert(33)
        h.insert(36)
        h.insert(35)

        Assert.assertEquals(h.size(), 7)
        Assert.assertEquals(h.findMin().toLong(), 29L)
        Assert.assertEquals(h.size(), 7)
        Assert.assertEquals(h.deleteMin().toLong(), 29L)
        Assert.assertEquals(h.size(), 6)
        Assert.assertEquals(h.findMin().toLong(), 30L)
        Assert.assertEquals(h.deleteMin().toLong(), 30L)
        Assert.assertEquals(h.findMin().toLong(), 30L)
        Assert.assertEquals(h.deleteMin().toLong(), 30L)
        Assert.assertEquals(h.findMin().toLong(), 31L)
        Assert.assertEquals(h.deleteMin().toLong(), 31L)
        Assert.assertEquals(h.findMin().toLong(), 33L)
        Assert.assertEquals(h.deleteMin().toLong(), 33L)
        Assert.assertEquals(h.findMin().toLong(), 35L)
        Assert.assertEquals(h.deleteMin().toLong(), 35L)
        Assert.assertEquals(h.findMin().toLong(), 36L)
        Assert.assertEquals(h.deleteMin().toLong(), 36L)
        Assert.assertEquals(h.size(), 0)
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    fun testSortRandomSeed1() {
        val h: Heap<Int> = IntegerRadixHeap(0, SIZE + 1)

        val generator = Random(1)

        val a = IntArray(SIZE)
        for (i in 0 until SIZE) {
            a[i] = (SIZE * generator.nextDouble()).toInt()
        }
        Arrays.sort(a)
        for (i in 0 until SIZE) {
            h.insert(a[i])
        }

        var prev: Int? = null
        var cur: Int
        while (!h.isEmpty) {
            cur = h.findMin()
            h.deleteMin()
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur) <= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSort2RandomSeed1() {
        val h: Heap<Int> = IntegerRadixHeap(0, SIZE + 1)

        val generator = Random(1)

        val a = IntArray(SIZE)
        for (i in 0 until SIZE) {
            a[i] = (SIZE * generator.nextDouble()).toInt()
        }
        Arrays.sort(a)
        for (i in 0 until SIZE) {
            h.insert(a[i])
        }

        var prev: Int? = null
        var cur: Int
        while (!h.isEmpty) {
            cur = h.deleteMin()
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur) <= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSortRandomSeed2() {
        val h: Heap<Int> = IntegerRadixHeap(0, SIZE + 1)

        val generator = Random(2)

        val a = IntArray(SIZE)
        for (i in 0 until SIZE) {
            a[i] = (SIZE * generator.nextDouble()).toInt()
        }
        Arrays.sort(a)
        for (i in 0 until SIZE) {
            h.insert(a[i])
        }

        var prev: Int? = null
        var cur: Int
        while (!h.isEmpty) {
            cur = h.findMin()
            h.deleteMin()
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur) <= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSort2RandomSeed2() {
        val h: Heap<Int> = IntegerRadixHeap(0, SIZE + 1)

        val generator = Random(2)

        val a = IntArray(SIZE)
        for (i in 0 until SIZE) {
            a[i] = (SIZE * generator.nextDouble()).toInt()
        }
        Arrays.sort(a)
        for (i in 0 until SIZE) {
            h.insert(a[i])
        }

        var prev: Int? = null
        var cur: Int
        while (!h.isEmpty) {
            cur = h.deleteMin()
            if (prev != null) {
                Assert.assertTrue(prev.compareTo(cur) <= 0)
            }
            prev = cur
        }
    }

    @Test
    fun testSameMinMax() {
        val h: Heap<Int> = IntegerRadixHeap(100, 100)

        for (i in 0..14) {
            h.insert(100)
        }

        Assert.assertEquals(15, h.size())
        for (i in 0..14) {
            Assert.assertEquals(100L, h.deleteMin().toLong())
        }
        Assert.assertEquals(0, h.size())
    }

    @Test
    fun testMultipleDeleteMin() {
        val step = 7
        val min = 0
        val max = 100000

        val h: Heap<Int> = IntegerRadixHeap(min, max)

        h.insert(min)
        var cur = min
        while (cur < max) {
            Assert.assertEquals(cur.toDouble(), h.findMin().toDouble(), 1e-9)
            if (cur + step >= max) {
                break
            }
            val newCur = cur + step
            h.insert(newCur)
            Assert.assertEquals(cur.toDouble(), h.findMin().toDouble(), 1e-9)
            Assert.assertEquals(cur.toDouble(), h.deleteMin().toDouble(), 1e-9)
            cur = newCur
        }
    }

    @Test
    fun testMaxDifference() {
        val h: Heap<Int> = IntegerRadixHeap(0, Int.MAX_VALUE)

        h.insert(0)
        h.insert(Int.MAX_VALUE)

        Assert.assertEquals(2, h.size())
        Assert.assertEquals(0, h.deleteMin().toLong())
        Assert.assertEquals(Int.MAX_VALUE, h.deleteMin())
        Assert.assertEquals(0, h.size())
    }

    @Test
    fun testClear() {
        val h: Heap<Int> = IntegerRadixHeap(0, 15)

        for (i in 0..14) {
            h.insert(i)
        }

        h.clear()
        Assert.assertEquals(0L, h.size())
        Assert.assertTrue(h.isEmpty)
    }

    @Test
    fun testMonotoneOkOnLastDeleted() {
        val h: Heap<Int> = IntegerRadixHeap(0, 1000)
        h.insert(100)
        Assert.assertEquals(100, h.findMin().toLong())
        h.insert(99)
        Assert.assertEquals(99, h.findMin().toLong())
    }

    @Test(expected = IllegalArgumentException::class)
    fun testMonotoneNotOkOnLastDeleted() {
        val h: Heap<Int> = IntegerRadixHeap(0, 1000)
        h.insert(100)
        Assert.assertEquals(100, h.findMin().toLong())
        h.deleteMin()
        h.insert(99)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalConstruction() {
        IntegerRadixHeap(-1, 100)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testIllegalConstruction1() {
        IntegerRadixHeap(100, 99)
    }

    @Test
    @Throws(IOException::class, ClassNotFoundException::class)
    fun testSerializable() {
        var h = IntegerRadixHeap(0, 15)

        for (i in 0..14) {
            h.insert(i)
        }

        // write
        val baos = ByteArrayOutputStream()
        val oos = ObjectOutputStream(baos)
        oos.writeObject(h)
        oos.close()
        val data = baos.toByteArray()

        // read
        val ois = ObjectInputStream(ByteArrayInputStream(data))
        val o = ois.readObject()
        ois.close()
        h = o as IntegerRadixHeap

        for (i in 0..14) {
            Assert.assertEquals((15 - i).toLong(), h.size())
            Assert.assertEquals(i, h.findMin())
            h.deleteMin()
        }
        Assert.assertTrue(h.isEmpty)
    }

   /* @Test(expected = IllegalArgumentException::class)
    fun testBadInsert() {
        IntegerRadixHeap(0, 15).insert(null)
    }*/

    @Test(expected = IllegalArgumentException::class)
    fun testBadInsert1() {
        IntegerRadixHeap(10, 15).insert(9)
    }

    @Test(expected = IllegalArgumentException::class)
    fun testBadInsert2() {
        IntegerRadixHeap(10, 15).insert(16)
    }

    @Test(expected = NoSuchElementException::class)
    fun testBadDeleteMin() {
        IntegerRadixHeap(10, 15).deleteMin()
    }

    @Test(expected = NoSuchElementException::class)
    fun testBadFindMin() {
        IntegerRadixHeap(10, 15).findMin()
    }

    companion object {
        private const val SIZE = 100000
    }
}
